import Vue from 'vue'
import Router from 'vue-router'
// 导入刚才编写的组件
import AppIndex from '../components/home/AppIndex'
import Login from '../components/Login'
import AppManager from '../components/views/AppManager'
import AppAdminInit from '../components/views/AppAdminInit'
import appDetails from '../components/views/AppDetails'
import groupDetails from '../components/views/GroupDetails'
import AppList from '../components/views/AgentAppList'
import AgentDetail from "../components/views/AgentDetail";
import VerbRegistry from "../components/views/registry/VerbRegistry"
import ActivityRegistry from "../components/views/registry/ActivityRegistry";
import CountIndex from "../components/count/CountIndex"
import SearchTimes from "../components/views/data/SearchTimes"
import DataIndex from "../components/count/DataIndex"
import GroupManager from '../components/views/GroupManager'
import UserManager from '../components/views/UserManager'
import AppAnalyse from '../components/views/AppAnalyse'
import AgentAnalyse from '../components/views/AgentAnalyse'
import GroupAnalyse from '../components/views/GroupAnalyse'

Vue.use(Router)

const router = new Router({
  mode: 'history',
  routes: [
    // 下面都是固定的写法
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/app_admin/init',
      name: 'appAdminInit',
      component: AppAdminInit
    },
    {
      path: '/',
      name: 'AppIndex',
      component: AppIndex,
      children: [
        {
          path: '/',
          name: 'CountIndex',
          component: CountIndex
        },
        {
          path: '/app/manager',
          name: 'appManager',
          component: AppManager
        }, {
          path: 'app/details',
          name: 'appDetails',
          component: appDetails
        }, {
          path: 'group/details',
          name: 'groupDetails',
          component: groupDetails
        }, {
          path: 'group/manager',
          name: 'groupManager',
          component: GroupManager
        }, {
          path: 'user/manager',
          name: 'userManager',
          component: UserManager
        }, {
          path: '/app/list',
          name: 'appList',
          component: AppList
        }, {
          path: '/agent/detail',
          name: 'agentDetail',
          component: AgentDetail
        }, {
          path: '/verb',
          name: 'verb',
          component: VerbRegistry
        }, {
          path: '/activity',
          name: 'activity',
          component: ActivityRegistry
        }, {
          path: '/searchTimes',
          name: 'searchTimes',
          component: SearchTimes
        },
        {
          path: '/dataIndex',
          name: 'DataIndex',
          component: DataIndex
        },
        {
          path: '/app/analyse',
          name: 'AppAnalyse',
          component: AppAnalyse
        },
        {
          path: '/agent/analyse',
          name: 'AgentAnalyse',
          component: AgentAnalyse
        },
        {
          path: '/group/analyse',
          name: 'GroupAnalyse',
          component: GroupAnalyse
        }

      ]
    }
  ]
});
// 导航守卫
// 使用 router.beforeEach 注册一个全局前置守卫，判断用户是否登陆
router.beforeEach((to, from, next) => {
  let token = localStorage.getItem('Authorization');
  if (to.path === '/login') {
    if (token === null || token === "") {
      next();
    } else {
      next('');
    }
  } else {
    if (token === null || token === "") {
      console.log("未登录")
      next('/login');
    } else {
      next();
    }
  }
});
export default router

